package com.example.college;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;









import android.os.AsyncTask;
import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.app.LauncherActivity.ListItem;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class Courses extends ListActivity {

	
	
	
	    // Progress Dialog
		private ProgressDialog pDialog;

		// Creating JSON Parser object
		JSONParser jParser = new JSONParser();

		ArrayList<HashMap<String, String>> coursesList;

		// url to get all Courses list
		private static String url_all_courses = "http://192.168.140.1:8080/WebApplication2/webresources/course/";

		// JSON Node names
		private static final String TAG_SUCCESS = "success";
		private static final String TAG_COURSES = "courses";
		private static final String TAG_CID = "cid";
		private static final String TAG_NAME = "name";
        private static final String TAG_CHECKBOX = "checkbox";

		// courses JSONArray
		JSONArray courses = null;
		ListView listView;
	
	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_courses);
		listView = (ListView) findViewById(android.R.id.list);
		listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		//listView.setItemChecked(, true);
		
		
		
		// Hashmap for ListView
		coursesList = new ArrayList<HashMap<String, String>>();
		
		// Loading courses in Background Thread
				new LoadAllCourses().execute();

		
		
			
			
			
			
			
			
		
		
	}

	
	
	/**
	 * Background Async Task to Load all courses by making HTTP Request
	 * */
	
	class LoadAllCourses extends AsyncTask<String, String, String>{

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			pDialog = new ProgressDialog(Courses.this);
			pDialog.setMessage("Loading Courses. Please wait...");
			pDialog.setIndeterminate(false);
			pDialog.setCancelable(false);
			pDialog.show();
			
		}
		
		
		
		/**
		 * getting All courses from url
		 * */
		
		@Override
		protected String doInBackground(String... arg0) {
			// Building Parameters
				List<NameValuePair> params = new ArrayList<NameValuePair>();

				
				
				// getting JSON string from URL
				JSONObject json = jParser.makeHttpRequest(url_all_courses, "GET", params);
				
					

				// Check your log cat for JSON reponse
						Log.d("All courses: ", json.toString());
						
						try {
							// Checking for SUCCESS TAG
							//int success = json.getInt(TAG_SUCCESS);

							if (json!= null) {
								// courses found
								// Getting Array of Courses
								//courses = json.getJSONArray(TAG_REG_COURSES);

								// looping through All Products
								
									// Storing each json item in variable
									
								
//									

									// creating new HashMap
									HashMap<String, String> map = new HashMap<String, String>();

									// adding each child node to HashMap key => value
									
									map.put(TAG_NAME, json.getString("c1"));

									// adding HashList to ArrayList
									coursesList.add(map);
									
									map = new HashMap<String, String>();
									
									map.put(TAG_NAME, json.getString("c2"));

									// adding HashList to ArrayList
									coursesList.add(map);
									
									

									// adding HashList to ArrayList
									coursesList.add(map);
									
									map = new HashMap<String, String>();
									
									map.put(TAG_NAME, json.getString("c3"));

									// adding HashList to ArrayList
									coursesList.add(map);
									
									map = new HashMap<String, String>();
									
									map.put(TAG_NAME, json.getString("c4"));

									// adding HashList to ArrayList
									coursesList.add(map);
									
									
									map = new HashMap<String, String>();
									
									map.put(TAG_NAME, json.getString("c5"));

									// adding HashList to ArrayList
									coursesList.add(map);
									
									map = new HashMap<String, String>();
									
									map.put(TAG_NAME, json.getString("c6"));

									// adding HashList to ArrayList
									coursesList.add(map);
									
								
								
							} else {
								// no registired courses found
								Toast toast= Toast.makeText(getApplicationContext(), "No Regstired Courses Found", Toast.LENGTH_LONG);
								toast.show();

								
							}
						} catch (JSONException e) {
							e.printStackTrace();
						}
						
						
						return null;
		}
		
		/**
		 * After completing background task Dismiss the progress dialog
		 * **/
		protected void onPostExecute(String file_url) {
			// dismiss the dialog after getting all products
			pDialog.dismiss();
			// updating UI from Background Thread
			runOnUiThread(new Runnable() {
				public void run() {
					/**
					 * Updating parsed JSON data into ListView
					 * */
					ListAdapter adapter = new SimpleAdapter(
							Courses.this, coursesList,
							android.R.layout.simple_list_item_multiple_choice, new String[] { TAG_NAME},
							new int[] { android.R.id.text1});
					
					
					
					
					
					
					// updating list view
					setListAdapter(adapter);
				}
			});
		}
		

}
	



}
